import json
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, Date
from models import BaseModels


class Users(BaseModels):
    __tablename__ = 'users'
    id = Column(Integer, autoincrement=True, primary_key=True, unique=True, nullable=False)
    account = Column(String(32), nullable=False, unique=True, comment="人员工号")
    name = Column(String(64), nullable=False, comment="人员名称")
    phone = Column(String(64), nullable=True, comment="人员电话号码")
    framework = Column(String(128), nullable=False, comment="人员组织架构")
    password = Column(String(256), nullable=False, comment="用户登录密码， hash256")
    role_id = Column(Integer, ForeignKey("roles.id"), comment="权限等级")
    time = Column(DateTime, nullable=False, comment="用户创建时间")
    enable = Column(Boolean, nullable=False, comment="是否可以登录系统")
    modify = Column(DateTime, nullable=False, comment="最后更改用户信息时间")

    def __repr__(self):
        return json.dumps({
            "user_id": self.id,
            "account": self.account,
            "name": self.name,
            "phone": self.phone,
            "framework": self.framework,
            "password": self.password,
            "role_id": self.role_id,
            "time": self.time.strftime("%Y-%m-%d %H:%M:%S") if self.time else None,
            "enable": True if self.enable is True else False,
            "modify": self.modify.strftime("%Y-%m-%d %H:%M:%S") if self.modify else None
        })
